﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Furniture_Rental_System
{
    public partial class RentItemForm : Form
    {
        String currentLoggedInEmployeeId;
        String memId;
        List<String> itemsToRent;
        DateTime dateAndTimeOfTransaction;

        public RentItemForm(String employeeId)
        {
            InitializeComponent();
            this.currentLoggedInEmployeeId = employeeId;
            this.itemsToRent = new List<string>();
        }

        private void buttonSearchRent_Click(object sender, EventArgs e)
        {
            this.memId = this.textBoxMemberID.Text;
            
            //need to check item number is valid before adding it to this list
            //itemsToRent.Add(this.textBoxItemNumber.Text);

            DataBaseAccessor checkItemExists = new DataBaseAccessor();
            List<ListViewItem> returnedList = new List<ListViewItem>();

            if (this.textBoxItemNumber.Text == "")
            {
                MessageBox.Show("Must enter an Item Number.");
                return;
            }
            
            String[] types = { "itemNumber" };
            String[] values = { this.textBoxItemNumber.Text };
            returnedList = checkItemExists.furnitureLookUpAvailableItem(types, values);


            if (returnedList.Count != 0)
            {
                this.listViewFurniture.Items.Add(returnedList[0]);
                itemsToRent.Add(this.textBoxItemNumber.Text);
                this.textBoxItemNumber.Text = "";
                this.buttonCompleteRentItems.Visible = true;
                //Runs through the returned list of ListViewItems and adds them to the ListView
                //for (int i = 0; i < returnedList.Count; i++)
                //{
                    //this.listViewFurniture.Items.Add(returnedList[i]);
                    //itemsToRent.Add(this.textBoxItemNumber.Text);
                    //this.textBoxItemNumber.Text = "";
                    //this.buttonCompleteRentItems.Visible = true;
                //}
            }
            else
            {
                MessageBox.Show("Item is not available: " + this.textBoxItemNumber.Text);
                this.textBoxItemNumber.Text = "";
            }
                

            
           
        }

        private void buttonCompleteRentItems_Click(object sender, EventArgs e)
        {
            dateAndTimeOfTransaction = System.DateTime.Now;
            DataBaseAccessor dataBaseAccessorForRentItem = new DataBaseAccessor();

            String[] args = new String[3];
            args[0] = this.memId;
            args[1] = this.currentLoggedInEmployeeId;

            foreach (String currentItem in this.itemsToRent)
            {
                args[2] = currentItem;
                if (!dataBaseAccessorForRentItem.addNewRental(args, this.dateAndTimeOfTransaction))
                {
                    MessageBox.Show("Error during rental transaction.");
                }
                
            }

            MessageBox.Show("Item(s) have been rented to Member: " + this.memId);
            Close();
        }

        private void buttonMemIdOkay_Click(object sender, EventArgs e)
        {
            DataBaseAccessor checkMemberID = new DataBaseAccessor();
            if (checkMemberID.memberLookUpByMemberID(this.textBoxMemberID.Text))
            {
                this.textBoxMemberID.Enabled = false;
                this.buttonMemIdOkay.Enabled = false;
                this.listViewFurniture.Visible = true;
                this.buttonRentItem.Visible = true;
                this.textBoxItemNumber.Visible = true;
                this.labelItemNumber.Visible = true;
            }
            else
            {
                MessageBox.Show("This Member ID Does Not Exist");
                this.textBoxMemberID.Text = "";
            }
        }


       
    }
}
